<!--
 * @Description: 
 * @version: 
 * @Author: Eve
 * @Date: 2019-10-08 11:16:50
 * @LastEditors: Eve
 * @LastEditTime: 2019-10-08 12:22:20
 -->
<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    .demo1 {
      text-align: center;
    }

    .demo2 {
      margin: 0 auto;
      width: 200px;
    }

    .demo3 {
      width: -moz-fit-content;
      width: -webkit-fit-content;
      width: fit-content;
      margin: 0 auto;
    }

    .demo3>p {
      float: left;
    }

    .demo4 {
      display: flex;
      justify-content: center;
      background-color: red;
      clear: both;
    }

    .demo5 {
      position: relative;
    }

    .demo5>span {
      position: absolute;
      transform: translateX(-50%);
      left: 50%;
    }

    .demo6,
    .demo7 {
      position: relative;
      margin-top: 30px;
    }

    .demo6>span {
      position: absolute;
      width: 200px;
      left: 50%;
      margin-left: -100px;
    }

    .demo7>span {
      position: absolute;
      width: 300px;
      left: 0;
      right: 0;
      margin: auto;
    }

    .demo8 {
      margin-top: 100px;
      height: 80px;
      background-color: red;
      line-height: 80px;
    }
    .demo9{
      border:1px solid red;
      height: 80px;
    }

    .demo9::after, .demo9>span {
      display: inline-block;
      vertical-align: middle;
    }

    .demo9::after {
      content: '';
      height: 100%;
    }
  </style>
</head>

<body>
  <div class="demo1">
    <p>行内元素 设置父元素的text-align:center 实现水平居中
      行内元素常用 a span img input</p>
    <img src="./demo.png" alt="">
  </div>
  <div>
    <p class="demo2">若是块级元素, 该元素设置 margin:0 auto即可. 实现水平居中
      块级元素常用 div h1-h5 p ul li </p>
  </div>

  <div class="demo3">
    <p>若子元素包含 float:left 属性, 为了让子元素水平居中, 则可让父元素宽度设置为fit-content,并且配合margin</p>
  </div>

  <div class="demo4">
    <span>使用flex 布局, 可以轻松的实现水平居中, 子元素设置如下</span>
  </div>

  <div class="demo5">
    <span>
      使用CSS3中新增的transform属性
    </span>
  </div>

  <div class="demo6">
    <span>使用绝对定位方式, 以及负值的margin-left -1/2宽度 需要固定宽度值</span>
  </div>

  <div class="demo7">
    <span>使用绝对定位方式, 以及left:0;right:0;margin:0 auto; 需要固定宽度</span>
  </div>

  <!-- 垂直居中 -->

  <div class="demo8">
    <span>若元素是单行文本, 则可设置 line-height 等于父元素高度</span>
  </div>

  <div class="demo9">
    <span>若元素是行内块级元素, 基本思想是使用display: inline-block, vertical-align: middle和一个伪元素让内容块处于容器中央.</span>
  </div>
</body>

</html>